#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using System.IO;
#endregion

namespace MyXnaGame
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry Option1MenuEntry;
        MenuEntry Option2MenuEntry;
        MenuEntry Option3MenuEntry;
        MenuEntry Option4MenuEntry;

        enum Difficulty
        {
            Easy,
            Medium,
            Hard,
        }

        static Difficulty currentDifficulty = Difficulty.Medium;

        static string[] languages = { "C#", "Cobol", "VB.NET" };
        static int currentLanguage = 0;

        static bool toggleOption = true;

        static int favoriteNumber = 23;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            Option1MenuEntry = new MenuEntry(string.Empty);
            Option2MenuEntry = new MenuEntry(string.Empty);
            Option3MenuEntry = new MenuEntry(string.Empty);
            Option4MenuEntry = new MenuEntry(string.Empty);

            //SetMenuEntryText();

            MenuEntry back = new MenuEntry("Back");

            // Hook up menu event handlers.
            Option1MenuEntry.Selected += Option1MenuEntrySelected;
            Option2MenuEntry.Selected += Option2MenuEntryEntrySelected;
            Option3MenuEntry.Selected += Option3MenuEntryEntrySelected;
            Option4MenuEntry.Selected += Option4MenuEntryEntrySelected;
            back.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(Option1MenuEntry);
            MenuEntries.Add(Option2MenuEntry);
            MenuEntries.Add(Option3MenuEntry);
            MenuEntries.Add(Option4MenuEntry);
            MenuEntries.Add(back);

            // EasyStorage usage
            if (Global.SaveDevice.FileExists(Global.containerName, Global.fileName_options))
            {
                Global.SaveDevice.Load(
                    Global.containerName,
                    Global.fileName_options,
                    stream =>
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            currentLanguage = int.Parse(reader.ReadLine());
                            toggleOption = bool.Parse(reader.ReadLine());
                            favoriteNumber = int.Parse(reader.ReadLine());
                        }
                    });
            }
            SetMenuEntryText();
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            Option1MenuEntry.Text = "Difficulty: " + currentDifficulty;
            Option2MenuEntry.Text = "Language: " + languages[currentLanguage];
            Option3MenuEntry.Text = "Toggle This: " + (toggleOption ? "on" : "off");
            Option4MenuEntry.Text = "Pick number: " + favoriteNumber;
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Option 1 menu entry is selected.
        /// </summary>
        void Option1MenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentDifficulty++;

            if (currentDifficulty > Difficulty.Hard)
                currentDifficulty = 0;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Option 2 menu entry is selected.
        /// </summary>
        void Option2MenuEntryEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentLanguage = (currentLanguage + 1) % languages.Length;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Option 3 menu entry is selected.
        /// </summary>
        void Option3MenuEntryEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            toggleOption = !toggleOption;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Option 4 menu entry is selected.
        /// </summary>
        void Option4MenuEntryEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            favoriteNumber++;

            SetMenuEntryText();
        }


        #endregion

        protected override void OnCancel(PlayerIndex playerIndex)
        {
            // make sure the device is ready
            if (Global.SaveDevice.IsReady)
            {
                // save a file asynchronously. this will trigger IsBusy to return true
                // for the duration of the save process.
                Global.SaveDevice.SaveAsync(
                    Global.containerName,
                    Global.fileName_options,
                    stream =>
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.WriteLine(currentLanguage);
                            writer.WriteLine(toggleOption);
                            writer.WriteLine(favoriteNumber);
                        }
                    });
            }
            base.OnCancel(playerIndex);
        }
    }
}
